package cn.oa.desktop.newlabel.controller;

import cn.oa.desktop.newlabel.beans.BasePage;
import cn.oa.desktop.newlabel.beans.NewLabel;
import cn.oa.desktop.newlabel.service.NewLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/new")
public class NewController {
    @Autowired
    private NewLabelService newLabelService ;

    @RequestMapping("/newLabelAll")
    public ModelAndView NewLabelAll(@RequestParam(defaultValue = "1") Integer page){
        if (page<=0){
            page=1;
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("news/column");
        BasePage<NewLabel> newLabelBasePage = newLabelService.SelectPage(page);
        if(page>newLabelBasePage.getPageCount()){
            page=newLabelBasePage.getPageCount();
            BasePage<NewLabel> newLabelBasePage1 = newLabelService.SelectPage(page);
            modelAndView.addObject("newLabelBasePage",newLabelBasePage1);
        }else {
            modelAndView.addObject("newLabelBasePage", newLabelBasePage);
        }
        List<NewLabel> lists = newLabelService.SelectAll();
        modelAndView.addObject("lists",lists);

        return modelAndView;
    }

    @RequestMapping("/newLabelInsert")
    public String newLabelInsert(NewLabel newLabel){
        newLabelService.newInsert(newLabel);
        return "redirect:/new/newLabelAll.action";
    }

    @RequestMapping("/newLabelAl")
    public ModelAndView NewLabelAl(){
        ModelAndView modelAndView = new ModelAndView();
        List<NewLabel> newLabels = newLabelService.SelectAll();
        modelAndView.addObject("newLabels",newLabels);
        modelAndView.setViewName("news/columnInsert");

        return modelAndView;
    }
    @RequestMapping("/columnUpdate")
    public ModelAndView columnUpdate(String id){
        ModelAndView modelAndView = new ModelAndView();
        Integer Id = Integer.parseInt(id);
        NewLabel newLabel = newLabelService.SelectUpdate(id);
        modelAndView.addObject("newLabel",newLabel);
        modelAndView.setViewName("news/columnUpdate");
        List<NewLabel> newLabels = newLabelService.SelectAll();
        modelAndView.addObject("newLabels",newLabels);

        return modelAndView;
    }
    @RequestMapping("/newUpadte")
    public String newUpadte(NewLabel newLabel){
        newLabelService.NewUpdate(newLabel);
        return "redirect:/new/newLabelAll.action";
    }

    @RequestMapping("/columnDelete")
    public String columnDelete(String id){
        newLabelService.NewDelete(Integer.parseInt(id));
        return "redirect:/new/newLabelAll.action";
    }

    @RequestMapping("/newLab")
    @ResponseBody
    public NewLabel newLab(String name){
        NewLabel newLabel = newLabelService.SelectName(name);
        return newLabel;
    }

}
